package com.pub.tools;

public class FormatFunction {
    public FormatFunction() {
    }


    /* 轉成固定位數的文字字串, 空白加在前面 */
    public static String addSpaces(String str, int leng) {
        int i = 0;
        if (str.length() < leng) {
            i = leng - str.length();
            str = PublicFunction.spaces(i) + str;
        } else if (str.length() > leng) {
            str = str.substring(0, leng);
        }
        return str;
    } //End of addZeros

    /* 轉成固定位數的字串, 空白加在後面 */
    public static String appendSpaces(String str, int leng) {
        int i = 0;
        if (str.length() < leng) {
            i = leng - str.length();
            str = str + PublicFunction.spaces(i);
        } else if (str.length() > leng) {
            str = str.substring(0, leng);
        }
        return str;
    } //End of appendSpaces

    /* 目的：傳回固定位數的0 */
    public static String zeros(int len) {
        StringBuffer str = new StringBuffer("");
        for (int i = 0; i < len; i++) {
            str.append("0");
        }
        return str.toString();
    } //End of zeros

    /* 轉成固定位數的數字字串, 0 加在前面 */
    public static String addZeros(String str, int leng) {
        int i = 0;
        if (null == str) {
            str = "0";
        }
        if (str.length() < leng) {
            i = leng - str.length();
            str = zeros(i) + str;
        }
        return str;
    } //End of addZeros

    /* 轉成固定位數的數字字串, 0 加在後面 */
    public static String appendZeros(String str, int leng) {
        int i = 0;
        if (str.length() < leng) {
            i = leng - str.length();
            str = str + zeros(i);
        } else if (str.length() > leng) {
            str = str.substring(0, leng);
        }
        return str;
    } //End of appendZeros

    /*目的：將小數點後指定的位數刪除，並做補零的動作
      傳入：傳入的數字(String),小數點後取幾位 (直接捨去)
      傳回：格式化後的字串
      範例：String a = getNumToken("100.0633",2)
                  a = 100.06   */
    public static String getNumFormat(String strBd, int v) {
        String str;
        String s9 = "", v9 = "";
        if (strBd.indexOf(".") == -1) { //代表沒有小數點
            str = strBd + "." + appendZeros("0", v);
        } else {
            java.util.StringTokenizer st = new java.util.StringTokenizer(strBd.
                    toString(), ".");
            s9 = st.nextElement().toString();
            v9 = st.nextElement().toString();
            //long lngA=java.lang.StrictMath.round(Double.parseDouble(v9.substring(0,v+1))/10d); //小數點以下做四捨五入
            str = s9 + "." + appendZeros(v9, v);
        }
        return str;
    }

    /**
     * 回傳指定重複次數的字串
     *
     * @param pstrString 要重複的字串
     * @param pintRepeatNum 要重複的次數
     * @return String
     * @sample getRepeatStr("abc",3)="abcabcabc"
     */
    public static String getRepeatStr(String pstrString,int pintRepeatNum){
        StringBuffer lstbString=new StringBuffer();

        for(int i=1;i<=pintRepeatNum;i++){
            lstbString.append(pstrString);
        }

        return lstbString.toString();
    }
}
